clear

*install required packages if not installed
*ssc install estout

clear
insheet using "pooled_long.csv", comma
drop v1

foreach var of varlist dv dvpart unemp_county_true foreign_county_true value female college age* fraudexp nonwhite democrat republican {
capture replace `var'="" if `var'=="NA"
capture destring `var', replace
}

gen popbelief_acc=.

*national unemp:
replace popbelief_acc=(100-abs(value-4.5))/100 if issue=="unemp_US"

*local unemp:
replace popbelief_acc=(100-abs(value-unemp_county_true))/100 if issue=="unemp_county"

*national FB:
replace popbelief_acc=(100-abs(value-13.7))/100 if issue=="foreign_US"

*local FB:
replace popbelief_acc=(100-abs(value-foreign_county_true))/100 if issue=="foreign_county"

gen popbelief=value/100 if popbelief_acc!=.

tab dv if issue=="NEP"
tab dv if issue=="GMO"

gen issuenum=.
replace issuenum=1 if issue=="fraud"
replace issuenum=2 if issue=="warming"
replace issuenum=3 if issue=="militaryHC"
replace issuenum=4 if issue=="taxes"
replace issuenum=5 if issue=="unemp_US"
replace issuenum=6 if issue=="unemp_county"
replace issuenum=7 if issue=="foreign_US"
replace issuenum=8 if issue=="foreign_county"
replace issuenum=9 if issue=="NEP"
replace issuenum=10 if issue=="GMO"

bysort issuenum elite: su dv

** POOLED MODEL (DISCRETE OUTCOMES; DV = 0-1, where higher=more pro-GOP response)

egen respondent_id=group(respid)
xtset respondent_id

xtreg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus i.issuenum if issuenum==1 | issuenum==2 | issuenum==3 | issuenum==4, cluster(respid) robust
est store model1

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

** VOTER FRAUD MODEL (DV = 0-1, where higher=more pro-GOP response)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==1, robust
est store model2

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

** GLOBAL WARMING MODEL (DV = 0-1, where higher=more pro-GOP response)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==2, robust
est store model3

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite

** MILITARY/HC SPENDING MODEL (DV = 0-1, where higher=more pro-GOP response)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==3, robust
est store model4 

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

** TOP 1% TAXES MODEL (DV = 0-1, where higher=more pro-GOP response)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==4, robust
est store model5

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

estout model1 model2 model3 model4 model5, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

** POOLED MODEL (CONTINUOUS OUTCOMES; DV = 0-100, where higher=higher perceived levels)
xtreg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus i.issuenum if issuenum==5 | issuenum==7, cluster(respid) robust

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

** NAT'L UNEMPLOYMENT MODEL (DV = perceived local unemployment, 0-100)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==5, robust
est store model6

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

** FOREIGN BORN MODEL (DV = perceived foreign born population, 0-100)
reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==7, robust
est store model7 

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

estout model6 model7, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

*needle exchange

reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==9, robust
est store model9

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

*GMO

reg dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==10, robust
est store model10

*Dem/Rep mass polarization:
lincom 1.republican-1.democrat 
*Dem/Rep elite polarization:
lincom (1.republican+1.republican#1.elite)-(1.democrat+1.democrat#1.elite) 
*difference in mass/elite polarization:
lincom 1.republican#1.elite - 1.democrat#1.elite 

estout model9 model10, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

*Table 1/C2
estout model4 model5 model10 model2 model3 model9 model7 model6, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("R2 N")) starlevels(* 0.10 ** 0.05 *** 0.01) 


******************
*Appendix Table B*
******************

preserve
count
drop if issue=="NEP"
drop if issue=="GMO"
count

collapse (mean) dv female college nonwhite age* democrat republican elite, by(respid)
drop if dv==.
count

foreach var of varlist female college nonwhite {
list respid if `var'>0 & `var'<1
}

tab elite

*Table B1
bysort elite: tab female
bysort elite: tab college 
bysort elite: tab nonwhite 
bysort elite: su age*
bysort elite: su democrat if college!=. | nonwhite!=. | female!=. /*this avoids non-missing party variable problem*/
bysort elite: su republican if college!=. | nonwhite!=. | female!=. /*this avoids non-missing party variable problem*/

restore

preserve

count
keep if (issue=="NEP" | issue=="GMO") & dv!=.

collapse (mean) female college nonwhite age* democrat republican elite, by(respid)
tab elite

*Table B1

bysort elite: tab female
bysort elite: tab college 
bysort elite: tab nonwhite 
bysort elite: su age*
bysort elite: su democrat if college!=. | nonwhite!=. | female!=. /*this avoids non-missing party variable problem*/
bysort elite: su republican if college!=. | nonwhite!=. | female!=. /*this avoids non-missing party variable problem*/

restore

*Table B2
bysort issue elite: su dv


******************
*Appendix Table C*
******************

*oprobits
oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==3, robust
est store model4b 

oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==4, robust
est store model5b

oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==10, robust
est store model10b

oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==1, robust
est store model2b

oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==2, robust
est store model3b

oprobit dv democrat##elite republican##elite female college nonwhite age_3044 age_4564 age_65plus if issuenum==9, robust
est store model9b

*Table C1
estout model4b model5b model10b model2b model3b model9b, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(N, fmt(%9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

*Table C2 appears above


******************
*Appendix Table D*
******************

** LOCAL UNEMPLOYMENT MODEL
reg dv democrat##unempexp republican##unempexp female college nonwhite age_3044 age_4564 age_65plus unemp_county_true if issuenum==6 & elite==1, robust
est store model8

*Dem/Rep non-expert polarization:
lincom 1.republican-1.democrat 
*Dem/Rep expert polarization:
lincom (1.republican+1. republican#1.unempexp)-(1.democrat+1.democrat#1.unempexp) 
*difference in expert/non-expert polarization:
lincom 1.republican#1.unempexp - 1.democrat#1.unempexp 

** VOTER FRAUD MODELS (DV = perceived voter fraud, 0-1, where higher=more fraud))

* test 1: elected officials 
reg dv democrat##elected republican##elected female college nonwhite age_3044 age_4564 age_65plus if issuenum==1 & elite==1, robust
est store model11

*Dem/Rep non-expert polarization:
lincom 1.republican-1.democrat 
*Dem/Rep expert polarization:
lincom (1.republican+1.republican#1.elected)-(1.democrat+1.democrat#1.elected) 
*difference in expert/non-expert polarization:
lincom 1.republican#1.elected - 1.democrat#1.elected 

* test 2: officials who implement elections 

* define experience for voter fraud (experience = 1 if official works on implementing elections; 0 otherwise)
reg dv democrat##fraudexp republican##fraudexp female college nonwhite age_3044 age_4564 age_65plus if issuenum==1 & elite==1, robust
est store model12

*Dem/Rep non-expert polarization:
lincom 1.republican-1.democrat 
*Dem/Rep expert polarization:
lincom (1.republican+1.republican#1.fraudexp)-(1.democrat+1.democrat#1.fraudexp) 
*difference in non-expert/expert polarization:
lincom 1.republican#1.fraudexp - 1.democrat#1.fraudexp 

*Table D1
estout model8 model11 model12, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

* Create local question dummy:
gen local_quest = 0
replace local_quest = 1 if issuenum==6 | issuenum==8

*Unemployment model:
xtreg dv democrat##local_quest republican##local_quest elite female college nonwhite age_3044 age_4564 age_65plus unemp_county_true if issuenum==5 | issuenum==6, cluster(respid) robust
est store model13

*Dem/Rep national polarization:
lincom 1.republican-1.democrat 
*Dem/Rep local polarization:
lincom (1.republican+1.republican#1.local_quest)-(1.democrat+1.democrat#1.local_quest) 
*difference in local/national polarization:
lincom 1.republican#1.local_quest - 1.democrat#1.local_quest 

* Foreign born model:
xtreg dv democrat##local_quest republican##local_quest elite female college nonwhite age_3044 age_4564 age_65plus foreign_county_true if issuenum==7 | issuenum==8, cluster(respid) robust
est store model14

*Dem/Rep national polarization:
lincom 1.republican-1.democrat 
*Dem/Rep local polarization:
lincom (1.republican+1.republican#1.local_quest)-(1.democrat+1.democrat#1.local_quest)  
*difference in local/national polarization:
lincom 1.republican#1.local_quest - 1.democrat#1.local_quest 

*Table D2
estout model13 model14, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 


******************
*Appendix Table E*
******************

reg popbelief democrat##unempexp republican##unempexp female college nonwhite age_3044 age_4564 age_65plus unemp_county_true if issuenum==6 & elite==1, robust
est store model15

*Dem/Rep non-expert polarization:
lincom 1.republican-1.democrat 
*Dem/Rep expert polarization:
lincom (1.republican+1. republican#1.unempexp)-(1.democrat+1.democrat#1.unempexp) 
*difference in expert/non-expert polarization:
lincom 1.republican#1.unempexp - 1.democrat#1.unempexp 

*Table E1
estout model15, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 

*Unemployment model:
xtreg popbelief democrat##local_quest republican##local_quest elite female college nonwhite age_3044 age_4564 age_65plus unemp_county_true if issuenum==5 | issuenum==6, cluster(respid) robust
est store model16

*Dem/Rep national polarization:
lincom 1.republican-1.democrat
*Dem/Rep local polarization:
lincom (1.republican+1.republican#1.local_quest)-(1.democrat+1.democrat#1.local_quest) 
*difference in local/national polarization:
lincom 1.republican#1.local_quest - 1.democrat#1.local_quest 

* Foreign born model:
xtreg popbelief democrat##local_quest republican##local_quest elite female college nonwhite age_3044 age_4564 age_65plus foreign_county_true if issuenum==7 | issuenum==8, cluster(respid) robust
est store model17

*Dem/Rep national polarization:
lincom 1.republican-1.democrat 
*Dem/Rep local polarization:
lincom (1.republican+1.republican#1.local_quest)-(1.democrat+1.democrat#1.local_quest) 
*difference in local/national polarization:
lincom 1.republican#1.local_quest - 1.democrat#1.local_quest 

*Table E2
estout model16 model17, label style(tex) replace varwidth(25) collabels("") cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) stats(r2 N, fmt(%9.2f %9.0f) labels("N")) starlevels(* 0.10 ** 0.05 *** 0.01) 
